<template>
  <div
    class="business-page-container"
    v-loading="loading"
    element-loading-text="数据加载中..."
  >
    <iframe
      v-if="!loading && url"
      :src="url"
      style="border: none; box-shadow: none;"
      allowtransparency="yes"
      width="100%"
      height="100%"
    ></iframe>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from "vue-router";
import { useRouterStore } from "@/business/pinia/router";
import api from "@/http/api";
import {ElMessage} from "element-plus";

const loading = ref(true);
const route = useRoute();
const routerStore = useRouterStore();
const url = ref('');

onMounted(async() => {
  loading.value = true;

  try {
    const routeData = routerStore.getRouteData(route.name);
    const data = routeData?.data || {};
    if (data.linkSource === '1') {
      url.value = data.linkUrl;
    } else if (data.linkSource === '2') {
      if (data.linkScriptId) {
        const res = await api.runScript(data.linkScriptId);
        url.value = res.data;
      }
    }

    if (!url.value) {
      ElMessage.error("未配置外链地址！");
      return false;
    }
  } catch (e) {
    loading.value = false;
  } finally {
    loading.value = false;
  }
});
</script>

<style scoped>
.business-page-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}
</style>